//
//  VCNextRegisterView.m
//  VCShop
//
//  Created by VoyageCN on 16/8/18.
//  Copyright © 2016年 VoyageCN. All rights reserved.
//

#import "VCNextRegisterView.h"

@interface VCNextRegisterView ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *tostLabel;
@property (nonatomic, strong) UILabel *textBackLable;
@property (nonatomic, strong) UITextField *verificationText;
@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UIButton *registerBtn;
@property (nonatomic, strong) UIButton *countBtn;

@end

@implementation VCNextRegisterView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.tostLabel];
        [self addSubview:self.textBackLable];
        [self addSubview:self.verificationText];
        [self addSubview:self.lineLabel];
        [self addSubview:self.registerBtn];
        [self addSubview:self.countBtn];
    }
    return self;
}

- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    __weak typeof(self) weakSelf = self;
    
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@35);
        make.top.right.equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_left).offset(15);
    }];
    
    [_textBackLable mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.equalTo(@44);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
    }];
    
    [_verificationText mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.lineLabel.mas_left);
        make.top.bottom.equalTo(weakSelf.textBackLable);
    }];
    
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerY.equalTo(weakSelf.textBackLable.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(1, 30));
        make.right.equalTo(weakSelf.mas_right).offset(-102);
    }];
    
    [_registerBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.height.equalTo(@35);
        make.top.equalTo(weakSelf.verificationText.mas_bottom).offset(16);
    }];
    
    [_countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.size.mas_equalTo(CGSizeMake(100, 44));
        make.right.top.equalTo(weakSelf.textBackLable);
    }];

    
}

- (void)setPhoneNumText:(NSString *)phoneNumText{
    _phoneNumText = phoneNumText;
    _tostLabel.attributedText = [self makeTostAttributed];
}

- (UILabel *)tostLabel {
    
    if (!_tostLabel) {
        _tostLabel = [[UILabel alloc] init];
        _tostLabel.font = [UIFont systemFontOfSize:12.0];
    }
    return _tostLabel;
}

- (UILabel *)textBackLable {
    
    if (!_textBackLable) {
        _textBackLable = [[UILabel alloc] init];
        _textBackLable.backgroundColor = [UIColor whiteColor];
        _textBackLable.layer.borderWidth = 1;
        _textBackLable.layer.borderColor = RGB(218, 218, 218).CGColor;
    }
    return _textBackLable;
}

- (UITextField *)verificationText {
    
    if (!_verificationText) {
        _verificationText = [[UITextField alloc] init];
        _verificationText.delegate = self;
        _verificationText.placeholder = @"请输入验证码";
        [_verificationText addTarget:self action:@selector(verificationTextChangeText:) forControlEvents:UIControlEventEditingChanged];
    }
    return _verificationText;
}

- (UILabel *)lineLabel {
    
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc] init];
        _lineLabel.backgroundColor = RGB(231, 231, 231);
    }
    return _lineLabel;
}

- (UIButton *)registerBtn {
    
    if (!_registerBtn) {
        _registerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _registerBtn.backgroundColor = RGB(234, 234, 234);
        _registerBtn.userInteractionEnabled = NO;
        [_registerBtn setTitle:@"注 册" forState:UIControlStateNormal];
        [_registerBtn setTitleColor:RGB(142, 142, 142) forState:UIControlStateNormal];
        [_registerBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        _registerBtn.selected = NO;
        _registerBtn.layer.cornerRadius = 5.0;
        _registerBtn.layer.masksToBounds = YES;
        [_registerBtn addTarget:self action:@selector(registerMethod) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registerBtn;
}

- (UIButton *)countBtn {
    
    if (!_countBtn) {
        
        _countBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _countBtn.titleLabel.textAlignment = NSTextAlignmentCenter;
        _countBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
        [_countBtn addTarget:self action:@selector(startTimeRequest) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _countBtn;
}

#pragma mark - 制作属性字符串
//制作countBtn显示内容的方法
- (NSMutableAttributedString *)makeShowBtnText:(NSInteger)time{
    NSDictionary *attributedDic1 = @{NSForegroundColorAttributeName:RGB(56, 166, 241)};
    NSString *string = [NSString stringWithFormat:@"%li",time];
    NSAttributedString *attributedString1 = [[NSAttributedString alloc]initWithString:string attributes:attributedDic1];
    
    NSDictionary *attributedDic2 = @{NSForegroundColorAttributeName:RGB(133, 133, 133)};
    NSAttributedString *attributedString2 = [[NSAttributedString alloc]initWithString:@"秒后重试" attributes:attributedDic2];
    
    NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc]init];
    [mutableAttributed insertAttributedString:attributedString1 atIndex:0];
    [mutableAttributed insertAttributedString:attributedString2 atIndex:attributedString1.length];
    return mutableAttributed;
}
//制作提示文本属性字符串
- (NSAttributedString *)makeTostAttributed {
    
    NSDictionary *attributedDic1 = @{NSForegroundColorAttributeName:RGB(133, 133, 133)};
    NSAttributedString *attributedString1 = [[NSAttributedString alloc] initWithString:@"验证码已发送到" attributes:attributedDic1];
    
    NSDictionary *attributedDic2 = @{NSForegroundColorAttributeName:RGB(56, 166, 241)};
    NSString *string = [NSString stringWithFormat:@"+86 %@",_phoneNumText];
    NSAttributedString *attributedString2 = [[NSAttributedString alloc]initWithString:string attributes:attributedDic2];
    
    NSMutableAttributedString *mutableAttributed = [[NSMutableAttributedString alloc]init];
    [mutableAttributed insertAttributedString:attributedString1 atIndex:0];
    [mutableAttributed insertAttributedString:attributedString2 atIndex:attributedString1.length];
    return mutableAttributed;
}
#pragma mark - time
- (void)GCDTime {
    
    __block NSInteger time = 60;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        
        if (time < 2) {
            dispatch_source_cancel(timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                
                _countBtn.userInteractionEnabled = YES;
                NSDictionary *attributedDic1 = @{NSForegroundColorAttributeName:RGB(56, 166, 241)};
                NSAttributedString *attributedString1 = [[NSAttributedString alloc]initWithString:@"获取验证码" attributes:attributedDic1];
                [_countBtn setAttributedTitle:attributedString1 forState:(UIControlStateNormal)];
            });
        } else {
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                _countBtn.userInteractionEnabled = NO;
                [_countBtn setAttributedTitle:[self makeShowBtnText:time] forState:UIControlStateNormal];
            });
            time --;
        }
    });
    dispatch_resume(timer);
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    
    if (range.location == 6) {
        return NO;
    }
    return YES;
}

#pragma mark - private
- (void)verificationTextChangeText:(UITextField *)textField {
    
    if (textField.text.length == 6) {
        self.registerBtn.userInteractionEnabled = YES;
        self.registerBtn.backgroundColor = RGB(56, 166, 241);
        self.registerBtn.selected = YES;
    } else {
        self.registerBtn.userInteractionEnabled = NO;
        self.registerBtn.backgroundColor = RGB(229, 229, 229);
        self.registerBtn.selected = NO;
    }
}

- (void)startTimeRequest {
    
    if (_showTimeBlock) {
        _showTimeBlock();
    }
}

- (void)registerMethod {
    
    if (_registerBlock) {
        _registerBlock(_verificationText.text);
    }
}

@end
